#include "dialog_newaddgroup.h"
#include "ui_dialog_newaddgroup.h"
#include "messageboxutil.h"
#include <QDebug>

Dialog_newAddGroup::Dialog_newAddGroup(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_newAddGroup)
{
    ui->setupUi(this);

    m_iniManager = new iniManager("./config/workGroupConfig.ini");


    ui->productClassWidget->setStyleSheet("color:black");
    ui->lineEdit_workGroupId_set->setStyleSheet("color:black");

    connect(ui->productSizeWidget, &LineEditLWH::sizeChange, this, &Dialog_newAddGroup::do_LWH_To_currGroupId);


}

Dialog_newAddGroup::~Dialog_newAddGroup()
{
    delete ui;
}

void Dialog_newAddGroup::on_btn_enter_clicked()
{
    QString size = ui->productSizeWidget->getSize();

    if(size.isEmpty())
    {
        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("请先输入尺寸"));

        return;
    }

    QStringList sizeList = size.split("*");

    QString l = sizeList.at(0);
    QString w = sizeList.at(1);
    QString h = sizeList.at(2);

    // 获取当前工作组编号
    QString name = ui->lineEdit_workGroupId_set->text().trimmed();
    QString type;

    QStringList productClassList = ui->productClassWidget->getProductClass().split("_");

    qDebug() << "productClassList: " << productClassList;

    if(productClassList.size() != 2)
    {
        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("物料编号_工序编号 不能空!"));
        return;
    }

    QString materialNo = productClassList.at(0);
    QString workProcedureSequence = productClassList.at(1);
    // 获取产品类别
    QString productClass = ui->productClassWidget->getProductClass();

    // 物料编号
    if(materialNo.isEmpty())
    {
        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("物料编号不能空!"));

        return;
    }

    // 工序编号
    if(workProcedureSequence.isEmpty())
    {
        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("工序编号不能空!"));

        return;
    }

    // 长
    if(l.isEmpty())
    {
        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("长度不能空!"));

        return;
    }

    // 宽
    if(w.isEmpty())
    {
        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("宽度不能空!"));
        return;
    }

    // 高
    if(h.isEmpty())
    {
        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("高度不能空!"));

        return;
    }


//    QStringList groupIdList = iniManager::splitString(name);

//    if(name.isEmpty())
//    {
//        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("工作组编号不能为空!"));
//
//        return;
//    }


    // 查找编号是否存在
    if(m_iniManager->isExists("name", name))
    {
        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("工作组：%1 已存在，请勿重复添加!").arg(name));

        return;
    }



    QString isExists = m_iniManager->getGroup("productClass", productClass);

    // 查找产品类别是否存在
    if(!isExists.isEmpty())
    {
        // 已存在，绑定
        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("产品类别：%1\n已绑定配方：%2").arg(productClass).arg(isExists));

        qDebug() << QString("产品类别：%1 \t 已经绑定：%2").arg(productClass).arg(isExists);

        return;
    }

    emit sendSize(size);// 发送工件尺寸大小

    emit sendProductClass(productClass);// 发送工件类别

    accept();
}

void Dialog_newAddGroup::on_btn_cancel_clicked()
{
    reject();
}

void Dialog_newAddGroup::do_LWH_To_currGroupId(const QString &groupId)
{
    QString size = ui->productSizeWidget->getSize();

    if(size.isEmpty())
    {
//        MessageBoxUtil::hintMessage(this, MessageBoxUtil::Information, "警告", QString("请先输入尺寸"));
        qDebug() << "请先输入尺寸！";
        return;
    }

    QStringList sizeList = size.split("*");

    QString l = sizeList.at(0);
    QString w = sizeList.at(1);
    QString h = sizeList.at(2);

    QString name, type;


    m_Id = m_iniManager->readValue("current", "count").toString();

    if(!l.isEmpty() && !w.isEmpty())
    {
        if(l.toUInt() < 450 || w.toUInt() < 450){
            type = "Z";
        } else {
            type = "K";
        }
    }

    name = QString("%1_%2_%3_%4_%5").arg(m_Id).arg(l).arg(w).arg(h).arg(type);

    ui->comboBox_chainWidth_set->setCurrentText(iniManager::getGroupType(type));
    ui->lineEdit_workGroupId_set->setText(name);
}
